package com.jt.shop.service.impl;

import com.jt.shop.mapper.AddressMapper;
import com.jt.shop.mapper.UserMapper;
import com.jt.shop.model.Address;
import com.jt.shop.model.ShopException;
import com.jt.shop.model.User;
import com.jt.shop.service.AddressService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by he on 2015/9/5.
 */
@Service("addressService")
public class AddressServiceImpl implements AddressService {

    @Resource
    private AddressMapper addressMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public int add(Address address, int userId) {
        User user = userMapper.find(userId);
        if (user == null) {
            throw new ShopException("没找到用户, id=" + userId);
        }
        address.setUser(user);
        return addressMapper.add(address);
    }

    @Override
    public void update(Address address) {
        addressMapper.update(address);
    }

    @Override
    public void delete(int id) {
        addressMapper.delete(id);
    }

    @Override
    public Address find(int id) {
        return addressMapper.find(id);
    }

    @Override
    public List<Address> listByUserId(int userId) {
        return addressMapper.listByUserId(userId);
    }
}
